app.controller('HanMapCtrl', function ($scope, $stateParams, $modal, ajax, toaster) {
	/* global BMap */
	var inserter = new Req.POST({ resource: $scope.resource });

	$scope.pointArray = [];
	$scope.city = $stateParams.city;
	$scope.cityName = $stateParams.name;

	$scope.$on('han.map.init', function () {
		var map = $scope.map;
		map.centerAndZoom($scope.cityName + "市");
	});

	$scope.query = function () {
		var map = $scope.map;
		var area = $scope.area;
		var bdary = new BMap.Boundary();
		bdary.get(area, function (rs) {       //获取行政区域
			map.clearOverlays();        //清除地图覆盖物
			var count = rs.boundaries.length; //行政区域的点有多少个
			if (count === 0) {
				toaster.pop('error', '错误', '该区域不存在，请确认后再查询。');
				return;
			}
			var pointArray = [];
			for (var i = 0; i < count; i++) {
				var ply = new BMap.Polygon(rs.boundaries[i], { strokeWeight: 2, strokeColor: "#ff0000" }); //建立多边形覆盖物
				map.addOverlay(ply);  //添加覆盖物
				pointArray = pointArray.concat(ply.getPath());
			}
			map.setViewport(pointArray);    //调整视野
			$scope.pointArray = pointArray;
		});
	};

	$scope.add = function () {
		if ($scope.pointArray != "") {
			var modalInstance = $modal.open({
				templateUrl: 'template/rent/city/addArea.html?v=20d73a8981',
				controller: 'AreaModalCtrl',
				size: 'lg',
				resolve: {
					param: function () {
						return {
							"city": $stateParams.city,
							"type": $stateParams.type,
							"name": $scope.area
						};
					}
				}
			});

			modalInstance.result.then(function (param) {
				inserter.fields = param.data;
				inserter.datalist = $scope.pointArray;
				inserter.resource = 'city/rt/serviceArea/set';
				ajax.call(inserter).then(function (result) {
					if (result.code == 200) {
						toaster.pop('success', '成功', '新增信息成功。');
						$scope.pointArray = "";
					} else {
						toaster.pop('error', '错误', result.result);
					}
				}, function () {
					toaster.pop('error', '错误', '新增信息失败。');
				});

			});
		} else {
			toaster.pop('warning', '提示', '请先查询区域！');
		}
	};
});